function setupTutorGrid() {
    $("#tutors").jqGrid({
        url : 'getTutorList.action',
        datatype : "json",
        colNames : [ '序号', '姓名', '性别', '出生日期', '身份证号', '主讲课程', '讲师风格', '手机', '邮箱地址', '调用次数' ],
        colModel : [{ name : 'no', index : 'no', width : 25, sortable: false }, 
                    { name : 'name', index : 'name', width : 60 },
                    { name : 'isMale', index : 'isMale', width : 30 }, 
                    { name : 'birthday', index : 'birthday', width : 80 },
                    { name : 'idNo', index : 'idNo', width : 140 }, 
                    { name : 'major', index : 'major', width : 60 },
                    { name : 'style', index : 'style', width : 60 },
                    { name : 'cellPhone', index : 'cellPhone', width : 120 },
                    { name : 'mailAddress', index : 'mailAddress', idth : 120 },
                    { name : 'callTimes', index : 'callTimes', width : 60 }],
        rowNum : 10,
        height : 'auto',
        rowList : [ 10, 20, 30 ],
        pager : '#pager',
        sortname : 'id',
        viewrecords : true,
        sortorder : "desc",
        caption : "老师信息一览",
        multiselect : true,
        loadError : dealError
    });
    $("#tutors").jqGrid('navGrid', '#pager', {
        edit : false,
        add : false,
        del : false
    });
}

function setupTutorToolbar() {
    $("#add").button({
        icons : {
            primary : "ui-icon-plus"
        }
    }).click(function() {
        $("#id").val('');
        $("#detail").dialog("open");
    });

    $("#edit").button({
        icons : {
            primary : "ui-icon-pencil"
        }
    }).click(function() {
        var selects = $('#tutors').jqGrid('getGridParam', 'selarrrow');
        if (!selects || selects == '') {
            message('请选择需要更新的老师信息！', 'alert', 'top');
            return false;
        }
        if (selects.length > 1) {
            message('请只选择一条需要更新的老师信息！', 'alert', 'top');
            return false;
        }
        var params = {
            selectIds : selects.toString()
        };
        $.ajax({
            url : "updateInitTutor.action",
            type : "post",
            dataType : "json",
            data : params,
            success : showTutor,
            error : dealError
        });
    });

    $("#delete").button({
        icons : {
            primary : "ui-icon-close"
        }
    }).click(function() {
        var selects = $('#tutors').jqGrid('getGridParam', 'selarrrow');
        if (!selects || selects == '') {
            message('请选择需要删除的老师信息！', 'alert', 'top');
            return false;
        }
        confirm('是否确定删除？', 'center', doDeleteTutor);
    });

    $("#tutorProperty").button({
        icons : {
            primary : "ui-icon-zoomout"
        }
    }).click(function() {
        var selects = $('#tutors').jqGrid('getGridParam', 'selarrrow');
        if (!selects || selects == '') {
            message('请选择需要显示详细的老师信息！', 'alert', 'top');
            return false;
        }
        if (selects.length > 1) {
            message('请只选择一条需要显示详细的老师信息！', 'alert', 'top');
            return false;
        }
        $("#tutorId").val(selects[0]);
        $("#showDetailForm").submit();
    });

    $('#filter').button().click(function() {
        var sexCodeArray = getSexMatchCode($("#keyword").val());
        $('#unfilter').button('option', 'disabled', false);
        var tutorSearch = {
            'tutorSearch.idNo' : $("#keyword").val(),
            'tutorSearch.name' : $("#keyword").val(),
            'tutorSearch.sex' : -1,
            'tutorSearch.sexCodes' : sexCodeArray.join(),
            'tutorSearch.birthdayFrom' : '',
            'tutorSearch.birthdayTo' : '',
            'tutorSearch.address' : $("#keyword").val(),
            'tutorSearch.type' : $("#keyword").val(),
            'tutorSearch.major' : $("#keyword").val(),
            'tutorSearch.price' : '',
            'tutorSearch.style' : $("#keyword").val(),
            'tutorSearch.cellPhone' : $("#keyword").val(),
            'tutorSearch.imAddress' : $("#keyword").val(),
            'tutorSearch.phone' : $("#keyword").val(),
            'tutorSearch.mailAddress' : $("#keyword").val(),
            'tutorSearch.cellPhoneBak' : $("#keyword").val(),
            'tutorSearch.phoneBak' : $("#keyword").val(),
            'tutorSearch.mailAddressBak' : $("#keyword").val(),
            'tutorSearch.task' : $("#keyword").val(),
            'tutorSearch.introduction' : $("#keyword").val(),
            'tutorSearch.outLine' : $("#keyword").val(),
            'tutorSearch.remark' : $("#keyword").val(),
            'tutorSearch.isOr' : true
        };
        var postData = $("#tutors").jqGrid("getGridParam", "postData");
        $.extend(postData, tutorSearch);
        $("#tutors").jqGrid("setGridParam", {
            search: true
        }).trigger("reloadGrid", [{page:1}]);
    });
    $('#unfilter').button({ disabled : true }).click(function() {
        $(this).button('option', 'disabled', true);
        var tutorSearch = {
                'tutorSearch.idNo' : '',
                'tutorSearch.name' : '',
                'tutorSearch.sex' : -1,
                'tutorSearch.sexCodes' : '',
                'tutorSearch.birthdayFrom' : '',
                'tutorSearch.birthdayTo' : '',
                'tutorSearch.address' : '',
                'tutorSearch.type' : '',
                'tutorSearch.major' : '',
                'tutorSearch.price' : '',
                'tutorSearch.style' : '',
                'tutorSearch.cellPhone' : '',
                'tutorSearch.imAddress' : '',
                'tutorSearch.phone' : '',
                'tutorSearch.mailAddress' : '',
                'tutorSearch.cellPhoneBak' : '',
                'tutorSearch.phoneBak' : '',
                'tutorSearch.mailAddressBak' : '',
                'tutorSearch.task' : '',
                'tutorSearch.introduction' : '',
                'tutorSearch.outLine' : '',
                'tutorSearch.remark' : '',
                'tutorSearch.isOr' : true
            };
            var postData = $("#tutors").jqGrid("getGridParam", "postData");
            $.extend(postData, tutorSearch);
            $("#tutors").jqGrid("setGridParam", {
                search: true
            }).trigger("reloadGrid", [{page:1}]);
    });
}

function doDeleteTutor() {
    var selects = $('#tutors').jqGrid('getGridParam', 'selarrrow');
    var params = {
        selectIds : selects.toString()
    };
    $.ajax({
        url : "deleteTutor.action",
        type : "post",
        dataType : "json",
        data : params,
        beforeSend : validate,
        success : reload,
        error : dealError
    });
}

function validate(xhr) {
    ;
}

function validateTutor() {
    if ($('#idNo').val() == '') {
        message('请输入身份证号！', 'alert', 'top');
        return false;
    }
    if ($('#name').val() == '') {
        message('请输入姓名！', 'alert', 'top');
        return false;
    }
    return true;
}

function setupTutorDialog() {
    $("#detail").dialog({
        autoOpen : false,
        show : "slide",
        height : 600,
        width : 600,
        modal : true,
        buttons : {
            登录 : function() {
                $('#categoryId').val(2);
                var params = $("#tutorDetail").serialize();
                $.ajax({
                    url : "saveTutor.action",
                    type : "post",
                    dataType : "json",
                    data : params,
                    beforeSend : validateTutor,
                    success : doUpload,
                    error : dealError
                });
                return false;
            },
            取消 : function() {
                $(this).dialog("close");
                return false;
            }
        },
        close : function() {
            $("input").val("").removeClass("ui-state-error");
            $("select").val("").removeClass("ui-state-error");
            $("textarea").val("").removeClass("ui-state-error");
        }
    });
}

function doUpload() {
    if ($('#photo').val() != '') {
        var idNo = $('#idNo').val();
        ajaxPhotoUpload(idNo);
    }

    $('#detail').dialog('close');
    reload();
}

function reload() {
    $("#tutors").trigger("reloadGrid");
}

function showTutor(data, status) {
    $("#detail").dialog("open");
    $("#id").val(data.id);
    $("#name").val(data.name);
    $("#idNo").val(data.idNo);
    $("#sex").val(data.sex);
    $("#birthday").val(data.birthday);
    $("#address").val(data.address);
    $("#type").val(data.type);
    $("#major").val(data.major);
    $("#price").val(data.price);
    $("#style").val(data.style);
    $("#cellPhone").val(data.cellPhone);
    $("#imAddress").val(data.imAddress);
    $("#phone").val(data.phone);
    $("#mailAddress").val(data.mailAddress);
    $("#cellPhoneBak").val(data.cellPhoneBak);
    $("#phoneBak").val(data.phoneBak);
    $("#mailAddressBak").val(data.mailAddressBak);
    $("#task").val(data.task);
    $("#introduction").val(data.introduction);
    $("#outLine").val(data.outLine);
    $('#remark').val(data.remark);
    $('#isSigned').val(data.isSigned);
    $('#regDate').val(data.regDate);
    $('#categoryId').val(data.categoryId);
}

function ajaxPhotoUpload(idNo) {
    $("#loading").ajaxStart(function() {
        $(this).show();
    }).ajaxComplete(function() {
        $(this).hide();
    });
    $.ajaxFileUpload({
        url : 'tutorPhotoUpload.action?idNo=' + idNo,
        secureuri : false,
        fileElementId : 'photo',
        dataType : 'json',
        success : function(data, status) {
            if (data.errorMsg) {
                message('上传文件处理失败：' + data.errorMsg, 'alert', 'top');
            }
        }
    });
}

function setupTutorImport() {
    $("#upload").button({
        icons : {
            primary : "ui-icon-arrowthick-1-n"
        }
    }).click(function() {
        if ($('#tutorExcel').val() == '') {
            return false;
        }
        ajaxTutorImport();
    });

    $('#templateXls').button({
        icons : {
            primary: "ui-icon-copy"
        }
    });
    $('#templateDoc').button({
        icons : {
            primary: "ui-icon-copy"
        }
    });
}

function ajaxTutorImport() {
    $("#loading").ajaxStart(function() {
        $(this).show();
    }).ajaxComplete(function() {
        $(this).hide();
    });
    $.ajaxFileUpload({
        url : 'tutorExcelImport.action',
        secureuri : false,
        fileElementId : 'tutorExcel',
        dataType : 'json',
        data : {
            'excelTutor.categoryId' : $('#categoryId').val(),
            'excelTutor.remark' : $('#remark').val()
        },
        success : function(data, status) {
            if (data.errorMsg) {
                message('上传文件处理失败：' + data.errorMsg, 'alert', 'top');
            } else {
                message('上传文件处理成功', 'alert', 'top');
            }
        }
    });
}
